# require 'active_record/migrator'

APP_ROOT = Dir.pwd

desc "Setup the Environment by requiring pannonica and loading your init.rb"
task :pan_env do
  require 'pannonica'
  load APP_ROOT+'/conf/init.rb'
end

namespace :db do

  desc "Migrate the database through scripts in data/migrate. Target specific version with VERSION=x"
  task :migrate => :pan_env do
    ActiveRecord::Migrator.migrate("db/migrations/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
    Rake::Task["db:schema:dump"].invoke
  end
  
  namespace :schema do

    desc "Create a db/schema.rb file that can be portably used against any DB supported by AR"
    task :dump => :pan_env do
      require 'active_record/schema_dumper'
      File.open(ENV['SCHEMA'] || "db/schema.rb", "w") do |file|
        ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
      end
    end

    desc "Load a schema.rb file into the database"
    task :load => :pan_env do
      file = ENV['SCHEMA'] || "db/schema.rb"
      load(file)
    end
  end
end

